import React, { useEffect } from "react";
import { useLocation,useNavigate } from "react-router-dom";

//封装高阶组件实现路由守卫
function WithRouter(Cpn:React.ComponentType) {
    const NewCpn:React.FC = () => {
        const navigate=useNavigate();
        const {pathname}=useLocation();
        useEffect(() => {
            const token=localStorage.getItem('accessToken');
            if(!token && !["/login","/register"].includes(pathname)){
                navigate('/login');
            }
        },[pathname])
        return <Cpn/>
    };
    return NewCpn;
}
export default WithRouter;